const int pinMic = A0;
const int sampleWidth = 50;
int sample;

void setup() {
  // put your setup code here, to run once:
  pinMode(pinMic, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  long startTime = millis();
  int volume = 0;

  int sampleMax = 0;
  int sampleMin = 1024;

  while (millis() - startTime < sampleWidth) {
    
    sample = analogRead(pinMic);
    
    if (sample  > sampleMax) {
      sampleMax = sample;
    } else if ( sample < sampleMin) {
      sampleMin = sample;
    }

  }

  volume = sampleMax - sampleMin;
  Serial.println(volume);
}